package com.maila.xiaoaoschool.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.api.LoginApi;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.util.NullPointUtils;
import com.maila.xiaoaoschool.util.PrefUtil;
import com.maila.xiaoaoschool.util.RegularUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by Administrator on 2017/10/27.
 */

public class ForgetPwdActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.login_header)
    AutoLinearLayout loginHeader;
    @BindView(R.id.please_input_a_phonenum)
    EditText pleaseInputAPhonenum;
    @BindView(R.id.tv_zhanghao)
    TextView tvZhanghao;
    @BindView(R.id.send_verification_code)
    TextView sendVerificationCode;
    @BindView(R.id.vertical_view)
    View verticalView;
    @BindView(R.id.enter_the_verification_code)
    EditText enterTheVerificationCode;
    @BindView(R.id.please_input_a_password)
    EditText pleaseInputAPassword;
    @BindView(R.id.all_denglu)
    AutoLinearLayout allDenglu;
    @BindView(R.id.btn_register)
    Button btnRegister;
    private CountDownTimer timer;
    private String phonenum="";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                sendVerificationCode.setText("重新获取(" + (l / 1000) + ")");
                sendVerificationCode.setEnabled(false);
            }

            @Override
            public void onFinish() {
                sendVerificationCode.setEnabled(true);
                sendVerificationCode.setText("获取验证码");
            }
        };
        timer.start();

        initlistener();
    }
    private void initlistener() {
        enterTheVerificationCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if (enterTheVerificationCode.getText().length() > 0 && pleaseInputAPassword.getText().length() > 0) {
                    btnRegister.setEnabled(true);
                    btnRegister.setBackgroundResource(R.drawable.bg_rd_orange);
                } else {
                    btnRegister.setEnabled(false);
                    btnRegister.setBackgroundResource(R.drawable.bg_rd_gray);
                }
            }
        });
        pleaseInputAPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if (enterTheVerificationCode.getText().length() > 0 && pleaseInputAPassword.getText().length() > 0) {
                    btnRegister.setEnabled(true);
                    btnRegister.setBackgroundResource(R.drawable.bg_rd_orange);
                } else {
                    btnRegister.setEnabled(false);
                    btnRegister.setBackgroundResource(R.drawable.bg_rd_gray);
                }
            }
        });
    }
    @OnClick({R.id.white_return_iv, R.id.send_verification_code, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.send_verification_code:
                if(!NullPointUtils.checknull(pleaseInputAPhonenum.getText().toString())&& RegularUtils.isPhoneNumber(pleaseInputAPhonenum.getText().toString())){
                    timer.onTick(60000);
                    timer.start();
                    phonenum=pleaseInputAPhonenum.getText().toString();
                    new LoginApi(this, new VolleyInterface() {
                        @Override
                        public void onSuccess(String result) {
                            try {
                                JSONObject jsonObject=new JSONObject(result);
                                Toast.makeText(ForgetPwdActivity.this, ""+jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(VolleyError error) {

                        }
                    }).send_phone_code(pleaseInputAPhonenum.getText().toString());

                }else {
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_register:

                new  LoginApi(this, new VolleyInterface() {
                    @Override
                    public void onSuccess(String result) {
                        try {
                            JSONObject jsonObject=new JSONObject(result);
                            if (jsonObject.optInt("result")==1){
                                Toast.makeText(ForgetPwdActivity.this,"重置成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(VolleyError error) {

                    }
                }).reesetpwd(pleaseInputAPassword.getText().toString(),phonenum,enterTheVerificationCode.toString());
                break;
        }
    }
}
